package server;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;



public class TranslateThread implements Runnable {

    @Override
    public void run() {
        try {
            this.clientRun();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void clientRun() throws IOException {
        final ServerSocket server = new ServerSocket(33456);
        while (true) {
            System.out.println("开始监听。。。");
            Socket socket = null;
            socket = server.accept();
            System.out.println("有链接");
            receiveFile(socket);
        }
    }

    public static void receiveFile(Socket socket) throws IOException {
        byte[] inputByte = null;int length = 0;DataInputStream din = null;FileOutputStream fout = null;

        din = new DataInputStream(socket.getInputStream());
        fout = new FileOutputStream(new File("E:\\"+din.readUTF()));
        inputByte = new byte[1024];
        System.out.println("开始接收数据...");
        while (true) {
            if (din != null) length = din.read(inputByte, 0, inputByte.length);
            if (length == -1) break;
            System.out.println(length);
            fout.write(inputByte, 0, length);
            fout.flush();
        }
        System.out.println("完成接收");

    }
}
